package com.the_ring.chapter09.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.the_ring.chapter09.R;
import com.the_ring.chapter09.bean.NewsInfo;

import java.util.List;

public class RecyclerLinearAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<NewsInfo> publicList;

    public RecyclerLinearAdapter(Context mContext, List<NewsInfo> publicList) {
        this.mContext = mContext;
        this.publicList = publicList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_item, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder itemHolder = (ItemViewHolder) holder;
        itemHolder.picIv.setImageResource(publicList.get(position).picId);
        itemHolder.titleTv.setText(publicList.get(position).title);
        itemHolder.descTv.setText(publicList.get(position).desc);
        itemHolder.titleTv.setOnClickListener(v -> {
            // 点击事件
        });
    }

    @Override
    public int getItemCount() {
        return publicList.size();
    }

    // 定义列表项的试图持有者
    public class ItemViewHolder extends RecyclerView.ViewHolder {
        public ImageView picIv;
        public TextView titleTv;
        public TextView descTv;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            // picIv = itemView.findViewById(R.i)
        }
    }
}
